/**
 * 
 */
package com.seguriapp.dao;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.net.Uri;
import android.util.Log;

import com.seguriapp.providers.SeguriappProvider;
import com.seguriapp.util.Constants;

/**
 * @author pjohnson
 * 
 */
public class TramitesDao {

	public static final String TABLE_NAME = "Tramites";
	public static final String COL_ID = "_id";
	public static final String COL_TID = "tid";
	public static final String COL_NAME = "name";
	public static final String COL_COST = "cost";
	public static final String COL_HAS_FILE = "hasFile";
	public static final String COL_DURATION = "duration";

	// Content Provider constants
	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ SeguriappProvider.AUTHORITY + "/" + TABLE_NAME);
	public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.seguriapp.tramites";

	// Create the table
	protected static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME + " (" + COL_ID + " " + Constants.INTEGER
			+ " PRIMARY KEY," + COL_TID + " " + Constants.INTEGER + ", "
			+ COL_NAME + " " + Constants.TEXT + ", " + COL_HAS_FILE + " "
			+ Constants.INTEGER + ", " + COL_DURATION + " " + Constants.TEXT
			+ ", " + COL_COST + " " + Constants.TEXT + ")";

	public static ContentValues contentValuesToInsert(final JSONObject category) {
		final ContentValues values = new ContentValues();
		try {
			values.put(COL_TID, category.getInt("id"));
			values.put(COL_NAME, category.getString("name"));
			if (!category.isNull("cost")) {
				values.put(COL_COST, category.optString("cost"));
			}
			if (!category.isNull("duration")) {
				values.put(COL_DURATION, category.optString("duration"));
			}
			values.put(COL_HAS_FILE,
					"true".equals(category.getString("hasFile")) ? 1 : 0);

		} catch (JSONException e) {
			Log.e("error Json", e.getMessage());
		}
		return values;
	}

}
